package com.pq.controller;

import com.pq.pojo.order.OrderCart;
import com.pq.pojo.product.Product;
import com.pq.pojo.product.ProductBanner;
import com.pq.pojo.product.ProductImg;
import com.pq.pojo.user.User;
import com.pq.service.product.ProductService;
import com.pq.utils.CookieUtil;
import com.pq.utils.ObjectMapperUtil;
import com.pq.vo.SysResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Controller
public class ProductController {
    private static final String PQ_TICKET = "PQ_TICKET";

    @Autowired
    private ProductService productService;

    @Autowired
    private Jedis jedis;

    @ResponseBody
    @GetMapping("/doFindProductBanner")
    public SysResult doFindProductBanner() {
        List<ProductBanner> data = productService.findProductBanner();
        return SysResult.success(data);
    }

    @ResponseBody
    @GetMapping("/doFindProductList")
    public SysResult doFindProductList(String byFlag, String orderFlag, String pageCurrent) {
        int startIndex = 0;
        int rows = 4;
        List<Product> productList = productService.findProductList(byFlag, orderFlag, startIndex, rows);
        return SysResult.success(productList);
    }

    @GetMapping("/productdetail")
    public String findProductById(String productId, Model model) {
        model.addAttribute("productId",productId);
        return "productdetail";
    }

    @ResponseBody
    @GetMapping("/doFindProductDetailByProductId")
    public SysResult doFindProductDetailByProductId(String productId) {
        Product product = productService.findProductById(Integer.valueOf(productId));
        return SysResult.success(product);
    }

    @ResponseBody
    @GetMapping("/doFindImagesByProductId")
    public SysResult doFindImagesByProductId(String productId, String imgType) {
        ProductImg productImg = productService.findImagesByProductId(Integer.valueOf(productId), Integer.valueOf(imgType));
        return SysResult.success(productImg);
    }

    @GetMapping("/productlist")
    public String findProduct(@Param("logType") Integer logType,@Param("typeId") Integer typeId,Model model){
        model.addAttribute("logType",logType);
        model.addAttribute("typeId",typeId);
        return "productlist";
    }
    @RequestMapping("/doFindProductListByTypeId")
    @ResponseBody
    public SysResult doFindProductByTypeId(Integer typeId,String orderFlag,Integer pageCurrent){
        System.out.println(productService.findProductByList(typeId,orderFlag,pageCurrent));
        return productService.findProductByList(typeId,orderFlag,pageCurrent);
    }

    @ResponseBody
    @GetMapping("/doSaveOrderCart")
    public SysResult doSaveOrderCart(String productId, String productCounts,
                                     String productSizeId, HttpServletRequest request) {
        String ticket = CookieUtil.getCookieValue(request, PQ_TICKET);
        String json = jedis.get(ticket);
        User user = ObjectMapperUtil.toObject(json, User.class);
        int userId = user.getUserId();
        productService.doSaveOrderCart(Integer.valueOf(productId), Integer.valueOf(productCounts), userId);
        jedis.set("productId", productId);
        return SysResult.success();
    }

    @ResponseBody
    @PostMapping("/doSaveOrderPayDirectly")
    public SysResult doSaveOrderPayDirectly(String productId, String productCounts, HttpServletRequest request) {
        String ticket = CookieUtil.getCookieValue(request, PQ_TICKET);
        String json = jedis.get(ticket);
        User user = ObjectMapperUtil.toObject(json, User.class);
        int userId = user.getUserId();
        productService.doSaveOrderCart(Integer.valueOf(productId), Integer.valueOf(productCounts), userId);
        OrderCart orderCart = productService.doSaveOrderPayDirectly(Integer.valueOf(productId), Integer.valueOf(productCounts), userId);
        Integer orderId = new Random().nextInt(1000);
        jedis.set("orderId",String.valueOf(orderId));
        productService.insertOrderId(orderId, productId, orderCart.getOrderCartId());
        return SysResult.success("200", orderId);
    }

}
